package me.mstsc.util;

import java.util.Scanner;

/**
 * 工具类
 *
 * @author langle
 * @version 1.0
 */
public class ScannerInput {

    public static final Scanner SCANNER = new Scanner(System.in);

    /**
     * 获取 String
     *
     * @return String
     * @author langle
     */
    public static String getString() {
        if (SCANNER.hasNextLine()) return SCANNER.nextLine();
        return "";
    }

    /**
     * 打印 arg 并且获取 String
     *
     * @param arg:
     * @return String
     * @author langle
     */
    public static String getString(String arg) {
        System.out.print(arg);
        return getString();
    }

    /**
     * 打印 arg 并且获取 boolean
     *
     * @param arg:
     * @return boolean
     * @author langle
     */
    public static boolean getBoolean(String arg) {
        while (true) {
            String s = getString(arg + "[Y/N]: ").toLowerCase();
            if (s.matches("^[yn]$")) {
                return s.matches("^y$");
            } else {
                System.out.println("无效输入");
            }
        }
    }

    /**
     * 打印 arg 并且获取 int
     *
     * @param arg:
     * @return int
     * @author langle
     */
    public static int getInteger(String arg) {
        while (true) {
            String s = getString(arg);
            try {
                return Integer.parseInt(s);
            } catch (NumberFormatException e) {
                System.out.println("无效输入");
            }
        }
    }

}
